using System;
using System.Collections;
using System.Data;
using System.Threading;

namespace WebUpdateClient
{
	/// <summary>
	/// Summary description for Threads.
	/// Here we send emails ;)
	/// </summary>
	public class Threads
	{
		object[] updates = null;
		string StartupPath = "";
		string version = "";
		
		Form1.OutputDelegate outputDelegate = null;
		Form1.ProgressDelegate progressDelegate	= null;	
		
		public Threads(object[] updates, string StartupPath, string version, Form1.OutputDelegate outputDelegate, Form1.ProgressDelegate progressDelegate )
		{
			this.updates = updates;
			this.StartupPath = StartupPath;
			this.version = version;
			this.outputDelegate = outputDelegate;
			this.progressDelegate = progressDelegate;
		}

	
		public void Do()
		{
			
			int total = GetNumberToUpdate();
			int currentfilenumber = 0;

			try
			{
				this.progressDelegate( 0, total );
			}
			catch(Exception ex)
			{
				Console.WriteLine( ex.Message + "\r\n" + ex.StackTrace );
			}

			foreach ( object o in updates )
			{
				ws_update.UpdateInfos ui = (ws_update.UpdateInfos)o;
				ws_update.Update upd = new ws_update.Update();
				
				if ( !System.IO.File.Exists( StartupPath + ui.filepath ) || System.IO.File.GetCreationTimeUtc( StartupPath + ui.filepath ) < ui.time.ToUniversalTime() )
				{

					outputDelegate( "Currently downloading '" + ui.filepath + "' \r\n" );
										
					try
					{
						upd.Timeout = (1000*60)*15;
						ws_update.UpdateInfos ui2 = upd.GetFileCompressed( version, ui.filepath.Replace("\\", "/") );

						if (ui.filepath.ToUpper() != "\\WEBUPDATECLIENT.EXE" && ui.filepath.ToUpper() != "\\SHARPZIPLIB.DLL" && ui.filepath.ToUpper() != "\\COMPRESSIONEXTENSION.DLL")
						{

							//we try to create the directory
							string fullpath = StartupPath + ui.filepath;
							string fullpathwofilename = fullpath.Replace( System.IO.Path.GetFileName(fullpath), "" );
                            try
                            {
                                System.IO.Directory.CreateDirectory( fullpathwofilename );
                            }
                            catch
                            {
						
                            }
							
							if ( ui2.bin != null && ui2.bin.Length > 0 )
							{
								System.IO.FileStream fs2 = System.IO.File.Create( StartupPath + ui.filepath );
								fs2.Write( ui2.bin, 0, (int)ui2.bin.Length );
								fs2.Close();
								System.IO.File.SetCreationTimeUtc( StartupPath + ui.filepath, ui2.time.ToUniversalTime().AddMinutes(5));
							}

							
							outputDelegate( "Update for '" + ui.filepath + "' @ " + ui.time.ToUniversalTime() + " installed ! " + " \r\n" );
							
							try
							{
								currentfilenumber += 1;
								progressDelegate( currentfilenumber, total );
							}
							catch(Exception ex)
							{
								Console.WriteLine( ex.Message + "\r\n" + ex.StackTrace );
							}
						
						}
					}
					catch(Exception ex)
					{
						outputDelegate( "Non fatal error : " + ex.Message + " \r\n" );
					}

				}

				
				
		
			}

			outputDelegate( "\r\n Update finished ! " + " \r\n" );
			
			try
			{
				progressDelegate( total, total );
			}
			catch(Exception ex)
			{
				Console.WriteLine( ex.Message + "\r\n" + ex.StackTrace );
			}
		}
	

		public int GetNumberToUpdate()
		{
			int total = 0;

			foreach ( object o in updates )
			{
				ws_update.UpdateInfos ui = (ws_update.UpdateInfos)o;
				ws_update.Update upd = new ws_update.Update();
				
				if ( !System.IO.File.Exists( StartupPath + ui.filepath ) || System.IO.File.GetCreationTimeUtc( StartupPath + ui.filepath ) < ui.time.ToUniversalTime() )
				{
					total += 1;
				}
		
			}

			
			return total;
			
		}
	
	}
}
